package cn.mw.cmdb.virtualization.dto;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.virtualization.constant.MwVirtualizationConstant;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Map;

/**
 * @author gengjb
 * @description 虚拟化树结构数据DTO
 */
@Getter
@Setter
@Schema(name = "VirtualizationTreeDataDto", description = "虚拟化树结构数据DTO")
public class VirtualizationTreeDataDto implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "ID")
    private String id;

    @Schema(description = "父ID")
    private String pid;

    @Schema(description = "名称")
    private String name;

    @Schema(description = "主机ID")
    private String hostId;

    @Schema(description = "实例ID")
    private String instanceId;

    @Schema(description = "虚拟类型")
    private String vmType;

    @Schema(description = "引擎ID")
    private String engineId;

    @Schema(description = "数量")
    private Integer count;


    public void extractFrom(InstanceInfo instanceInfo, String instanceId){
        this.name = instanceInfo.getInstanceName();
        this.hostId = instanceInfo.getMonitorTypeId();
        this.instanceId = instanceInfo.getId();
        Map<String, Object> infoData = instanceInfo.getData();
        if(infoData.containsKey(MwVirtualizationConstant.PID)){
            this.pid = (String) infoData.get(MwVirtualizationConstant.PID);
        }
        if(infoData.containsKey(MwVirtualizationConstant.ID)){
            this.id = (String) infoData.get(MwVirtualizationConstant.ID);
        }
        if(infoData.containsKey(MwVirtualizationConstant.VM_TYPE)){
            this.vmType = (String) infoData.get(MwVirtualizationConstant.VM_TYPE);
        }
        if(infoData.containsKey(MwVirtualizationConstant.ENGINE_ID)){
            this.engineId = (String) infoData.get(MwVirtualizationConstant.ENGINE_ID);
        }
        if(this.pid != null && this.id != null && this.pid.equals(this.id)){
            this.pid = instanceId;
        }
    }
}
